﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ShopPowerTemplate.Common;
using ShopPowerTemplate.ViewModels;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using ShopPowerTemplate.Data;
using ShopPowerTemplate.Entities;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI.Popups;

// The Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

namespace ShopPowerTemplate
{
    /// <summary>
    /// A page that displays a collection of item previews.  In the Split Application this page
    /// is used to display and select one of the available groups.
    /// </summary>
    public sealed partial class ProductViewPage : ShopPowerTemplate.Common.LayoutAwarePage
    {
        public ProductViewPage()
        {
            this.InitializeComponent();
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            if (navigationParameter != null)
            {
                var product = navigationParameter as Product;
                var productPresenter = new ProductPresenterDetailViewModel(product);

                HookSharing((DataTransferManager sender, DataRequestedEventArgs args) => productPresenter.FillShareInfo(args.Request));
                this.DataContext = productPresenter;

                ((ProductPresenterDetailViewModel)this.DataContext).PropertyChanged += ProductViewPage_PropertyChanged; //Tito
            }
        }

        //Tito
        void ProductViewPage_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ProductOptions") /*&& (((ProductPresenterDetailViewModel)this.DataContext).ProductOptions != null))*/
            {
                UpdateButtonState();

                foreach (ProductOptionsViewModel po in ((ProductPresenterDetailViewModel)this.DataContext).ProductOptions)
                {
                    po.PropertyChanged += po_PropertyChanged;
                }
            }
        }

        void po_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedOption")
            {
                UpdateButtonState();
            }
        }

        private void UpdateButtonState()
        {
            bool hayStock = ((ProductPresenterDetailViewModel)this.DataContext).CanExecuteAddProduct();
            this.addToCartButton.IsEnabled = hayStock;
            this.addToCartButton.Content = hayStock ? "Añadir al carrito" : "No hay Stock";
        }
        //Fin Tito

        private void Informacion_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            informationGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
            ratingGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }

        private void Opiniones_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            informationGrid.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            ratingGrid.Visibility = Windows.UI.Xaml.Visibility.Visible;
        }

        private async void AddReview_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            if (await App.SignIn())
                this.Frame.Navigate(typeof(RatingViewPage), (this.DataContext as ProductPresenterDetailViewModel).Product);
            else
                await new MessageDialog("Debe iniciar sesión para añadir su valoración").ShowAsync();
        }


    }
}
